﻿using System;
using System.Collections;
using System.Collections.Generic;
using CSUtils;
using G02;
using U3dUtils;
using UnityEngine;

namespace TripleJump {
    public class CFirstPlayerInput {
        const string _tag = "CFirstPlayerInput";

        public void GameReset() {
        }

        public void GameStart() {
        }

        public void GameEnd() {
        }

        public CPara para = new CPara();

        public void Init() {
            CMonoMonitor.onUpdateEvent += _Update;
        }

        //手指按下位置
        Vector3 fingerPressPos;
        Vector3 roleInitialPos;
        Vector3 hitStartPoint;
        Vector3 fistRoleStartPoint;

        void _Update() {
//            if (TJTools.IsClickUI()) {
//                return;
//            }
            if (TJTools.IsEditor()) {
                //CLog.i(_tag, "手指按下检查...._Update().");
                if (Input.GetMouseButtonDown(0)) {
                    //CLog.i(_tag, "手指按下检查.........按下..");
                    _FingerPressEvent();
                    _FingerDragControl();
                }
                if (Input.GetMouseButtonUp(0)) {
                    //CLog.i(_tag, "手指按下检查............抬起..");
                    _FingerLiftEvent();
                }
                if (Input.GetMouseButton(0)) {
                    //CLog.i(_tag, "手指按下检查...ww..");
                    _FingerDragControl();
                }
            } else {
                if (Input.touchCount > 0) {
                    if (Input.GetTouch(0).phase == TouchPhase.Began) {
                        _FingerPressEvent();
                        _FingerDragControl();
                    } else if (Input.GetTouch(0).phase == TouchPhase.Ended) {
                        _FingerLiftEvent();
                    } else {
                        _FingerDragControl();
                    }
                }
            }
        }

        //手指按下事件
        void _FingerPressEvent() {
            fingerPressPos = Input.mousePosition;
            roleInitialPos = CGameCore.GetFirstHeroPos(); // fristRoleRoot.position;

            //lastHitPoint = hitStartPoint;
            fistRoleStartPoint = roleInitialPos;
            if (TJGameData.gameState == EGameState.e01_GameReset) {
                if (CPara.onGetVitalityValue() >= CPara.LevelVitalityCost) {
                    //UI_Main.S_AddStrength(-5, G01Manage.GetInstance.roleManage.mainRole.roleRoot);
                    CPara.onUIAddVitalityEvent(-CPara.LevelVitalityCost, roleInitialPos);
                    CPara.onGameStart();
                } else {
                    SystemTipsUI.TipsView(null, "体力值不足", "请充值，兑换体力值", null);
                }
            } else {
                CPara.onSetGameRuningSpeed(EGameRuningSpeedType.e01_normal);
                CPara.onFingerClick();
            }
        }

        //手指抬起事件
        void _FingerLiftEvent() {
            if (TJGameData.gameState == EGameState.e05_GameEnd) {
                // G01Manage.GetInstance.GameStart();
            } else {
                CPara.onSetGameRuningSpeed(EGameRuningSpeedType.e02_slow);
                CPara.onFingerLift();
            }
        }

        //手指拖动控制
        void _FingerDragControl() {
            if (TJGameData.gameState == EGameState.e05_GameEnd) {
                return;
            }
        }

        public class CPara {
            public float dragLerp = 8;
            /// <summary>
            /// buff减速倍率
            /// </summary>
            public static float buff_slowDownRatio;

            /// <summary>
            /// 添加体力值
            /// </summary>
            public static Action<long, Vector3> onUIAddVitalityEvent;
            /// <summary>
            /// 开始游戏
            /// </summary>
            public static Action onGameStart;
            public static Action onFingerClick;
            public static Action onFingerLift;
            /// <summary>
            /// 设置游戏运行速度
            /// </summary>
            public static Action<EGameRuningSpeedType> onSetGameRuningSpeed;
            /// <summary>
            /// 获取体力值
            /// </summary>
            public static Func<long> onGetVitalityValue;
            /// <summary>
            /// 关卡体力消耗
            /// </summary>
            public static int LevelVitalityCost = 5;
            /// <summary>
            /// 过关奖励体力值
            /// </summary>
            public static int PassLevelVitalityReward = 5;
        }
    }
}
